package com.nbcio.pm.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.common.satoken.utils.LoginHelper;
import com.nbcio.pm.domain.bo.PmProjectBo;
import com.nbcio.pm.domain.vo.PmProjectVo;
import com.nbcio.pm.service.IPmProjectService;
import com.nbcio.pm.service.impl.PmProjectValidationService;
import com.nbcio.pm.domain.vo.PmProjectMemberVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 项目管理Controller
 *
 * @author nbacheng
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/project")
@Tag(name = "项目管理", description = "项目管理相关接口")
public class PmProjectController extends BaseController {

    private final IPmProjectService pmProjectService;
    private final PmProjectValidationService validationService;

    /**
     * 查询项目列表
     */
    @Operation(summary = "查询项目列表", description = "分页查询项目列表")
    @SaCheckPermission("pm:project:list")
    @GetMapping("/list")
    public TableDataInfo<PmProjectVo> list(PmProjectBo bo, PageQuery pageQuery) {
        return pmProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取项目详细信息
     */
    @Operation(summary = "获取项目详细信息", description = "根据项目ID获取详细信息")
    @SaCheckPermission("pm:project:query")
    @GetMapping("/{projectId}")
    public R<PmProjectVo> getInfo(@Parameter(description = "项目ID") @PathVariable Long projectId) {
        return R.ok(pmProjectService.queryById(projectId));
    }

    /**
     * 新增项目
     */
    @Operation(summary = "新增项目", description = "创建新项目")
    @SaCheckPermission("pm:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmProjectBo bo) {
        return toAjax(pmProjectService.insertByBo(bo));
    }

    /**
     * 修改项目
     */
    @Operation(summary = "修改项目", description = "更新项目信息")
    @SaCheckPermission("pm:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmProjectBo bo) {
        return toAjax(pmProjectService.updateByBo(bo));
    }

    /**
     * 删除项目
     */
    @Operation(summary = "删除项目", description = "删除项目")
    @SaCheckPermission("pm:project:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public R<Void> remove(@Parameter(description = "项目ID数组") @PathVariable Long[] projectIds) {
        return toAjax(pmProjectService.deleteWithValidByIds(Arrays.asList(projectIds), true));
    }

    /**
     * 更新项目进度
     */
    @Operation(summary = "更新项目进度", description = "更新项目完成进度")
    @SaCheckPermission("pm:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{projectId}/progress")
    public R<Void> updateProgress(@Parameter(description = "项目ID") @PathVariable Long projectId, 
                                  @Parameter(description = "进度百分比") @RequestParam BigDecimal progress) {
        return toAjax(pmProjectService.updateProgress(projectId, progress));
    }

    /**
     * 获取项目统计
     */
    @Operation(summary = "获取项目统计", description = "获取项目统计信息")
    @SaCheckPermission("pm:project:query")
    @GetMapping("/{projectId}/stats")
    public R<PmProjectVo> getStats(@Parameter(description = "项目ID") @PathVariable Long projectId) {
        return R.ok(pmProjectService.getProjectStats(projectId));
    }

    /**
     * 获取用户项目
     */
    @Operation(summary = "获取用户项目", description = "获取当前用户参与的项目列表")
    @GetMapping("/my-projects")
    public R<List<PmProjectVo>> getMyProjects() {
        Long userId = LoginHelper.getUserId();
        return R.ok(pmProjectService.getUserProjects(userId));
    }

    /**
     * 复制项目
     */
    @Operation(summary = "复制项目", description = "复制现有项目")
    @SaCheckPermission("pm:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/{projectId}/copy")
    public R<Void> copyProject(@Parameter(description = "项目ID") @PathVariable Long projectId, 
                               @Parameter(description = "新项目名称") @RequestParam String newProjectName) {
        return toAjax(pmProjectService.copyProject(projectId, newProjectName));
    }

    /**
     * 归档项目
     */
    @Operation(summary = "归档项目", description = "归档项目")
    @SaCheckPermission("pm:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{projectId}/archive")
    public R<Void> archiveProject(@Parameter(description = "项目ID") @PathVariable Long projectId) {
        return toAjax(pmProjectService.archiveProject(projectId));
    }

    /**
     * 获取项目成员列表
     */
    @Operation(summary = "获取项目成员列表", description = "获取指定项目的活跃成员列表")
    @SaCheckPermission("pm:project:list")
    @GetMapping("/{projectId}/members")
    public R<List<PmProjectMemberVo>> getProjectMembers(@Parameter(description = "项目ID") @PathVariable Long projectId) {
        List<PmProjectMemberVo> members = validationService.getActiveProjectMembers(projectId);
        return R.ok(members);
    }
}
